package com.example.springsecurity.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springsecurity.entity.SystemMenu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 菜单信息表(SystemMenu)表数据库访问层
 *
 * @author aman
 * @since 2025-01-15 16:59:00
 */
@Mapper
public interface SystemMenuMapper extends BaseMapper<SystemMenu> {

    /**
     * 根据用户ID查询权限
     * @param userId
     * @return
     */
    @Select("SELECT DISTINCT m.permission" +
            " FROM system_user_role ur" +
            " LEFT JOIN system_role r ON ur.role_id = r.id" +
            " LEFT JOIN system_role_menu rm ON ur.role_id = rm.role_id" +
            " LEFT JOIN system_menu m ON m.id = rm.menu_id" +
            " WHERE ur.user_id = #{userId}" +
            " AND r.status = 0" +
            " AND m.status = 0")
    List<String> selectPermsByUserId(@Param("userId") Long userId);

    @Select("SELECT COUNT(*) FROM system_menu WHERE system_menu.menu_name IN (#{menuNames})")
    int existsByMenuName(@Param("menuNames") List<String> menuNames);
}
